#ifndef THREAD_THREAD_H
#define THREAD_THERAD_H

#include "Latch.h"
#include "utility/func.h"
#include "utility/MoveOnly.h"

#include <atomic>
#include <functional>
#include <string>
#include <thread>


class Latch;

class Thread {
public: 
    static const std::atomic<int32_t> &TOTAL() { return _total; }

    explicit Thread(const TrivialFunc func, const std::string &name = {});
    ~Thread();

    MOVEONLY(Thread);

    bool is_started() const { return _started;}
    const std::string &name() const { return _name; }
    int tid() const { return _tid; }

    void start();
    void join();

private:
    static inline std::atomic<int32_t> _total{1};

    TrivialFunc _func;
    std::string _name;
    std::unique_ptr<Latch> _latch;
    std::unique_ptr<std::thread> _thread;
    int _tid;
    bool _started;
    bool _joined;
};


#endif